package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Activation;
import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.ActivationMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.UserQuery;
import cn.wolfcode.trip.base.service.IInformationService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.EmailController;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.InstanceNotFoundException;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IInformationService informationService;

    @Autowired
    private ActivationMapper activationMapper;


    @Override
    public void save(User user) {

        userMapper.insert(user);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> listAll(UserQuery qo) {
        qo.setPageSize(10);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectAll(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);

    }

    @Override
    public void register(User user) {
        //先判断邮箱存不存在,存在抛出异常,不存在则新增一个用户
        if (userMapper.selectByEmailAndPassword(user.getEmail(), null) != null) {
            throw new RuntimeException("邮箱已存在,请使用其他邮箱注册!");
        }
        userMapper.insert(user);

        /**
         * 用户注册完成后,发送邮件
         */
        System.out.println(user.getId());
        String s = UUID.randomUUID().toString();
        EmailController.sendFileMail("欢迎注册骡爸爸", "欢迎注册骡爸爸网站,你可以和十万用户一起嗨皮", s, user.getId(), user.getEmail());
        Activation activation = new Activation();
        activation.setUserId(user.getId());
        activation.setUuid(s);
        activationMapper.insert(activation);

        /**
         * 用户注册完毕之后,我需要给他发一条通知
         */
        Information information = new Information();
        information.setType(Information.SYSTEM_MESSAGE_ALL);
        information.setContent("恭喜用户" + user.getEmail() + "注册本网站,你可以在本网站做很多的事情.");
        information.setTitle("用户注册信息");
        informationService.saveUserInformation(information, user);
    }

    @Override
    public User login(User user) {
        if (user.getEmail() != null && user.getPassword() != null) {
            User currentUser = userMapper.selectByEmailAndPassword(user.getEmail(), user.getPassword());
            if (currentUser == null) {
                //如果没有查询到用户,则抛出异常
                throw new RuntimeException("你猜是密码错了还是邮箱错了!");
            }
            //成功登陆则把用户存在session中
            UserContext.setCurrentUser(currentUser);
            return currentUser;

        } else {
            throw new RuntimeException("邮箱或者密码不能为空");
        }
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveByUserIdAndFollowId(Long userId, Long followId) {
        userMapper.insertUserAndFollowRelation(userId, followId);
    }

    @Override
    public Integer selectRelationsByUserIdAndFollowID(Long userId, Long followId) {
        return userMapper.selectRelationsByUserIdAndFollowID(userId, followId);
    }

    @Override
    public void deleteRelation(Long followId) {
        userMapper.deleteRelation(followId);
    }

    @Override
    public int getFansByUserId(Long userId) {
        return userMapper.getFansByUserId(userId);
    }

    @Override
    public int getFollowsByFollowId(Long followId) {
        return userMapper.getFollowsByFollowId(followId);
    }

    @Override
    public List listTravelersById(Long userId) {
        return userMapper.listTravelersById(userId);
    }
}
